<?php
    session_start();
    require_once("../includes/conn.php");
    require_once "../includes/wxtoken.php";

    $sysconfig=$dbconn->fetch($dbconn->query("select * from ".DBQIAN."_sys_config where uid=1"));


    define('APPID',$sysconfig['cappid']);
    define('APPSECRET',$sysconfig['cappsecret']);
    $tools=new tools();
    if(!$tools->check_is_weixin())
    {
        echo '
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>入口不合法</title>
        </head>

        <body>
         错误：必须在微信环境下进入。
        </body>
        </html>
        ';
		exit;
    }
	$utocode=(isset($_GET['utocode']))?$_GET['utocode']:"";

    $wxtoken = new wxtoken(1);

    $signPackage = $wxtoken->GetSignPackage();

   $ucode= $_SESSION['ucode']?$_SESSION['ucode']:'';
   if(isset($_GET['code']) &&$_GET['code']!='')
    {
        $WXCODE=$_GET['code'];
        $getucodeurl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$WXCODE."&grant_type=authorization_code";
        $getucodejson=json_decode($tools->http_curl_get($getucodeurl,true));
        $wxlintoken=$getucodejson->access_token;
        $ucode=$getucodejson->openid;
        $getutxturl="https://api.weixin.qq.com/sns/userinfo?access_token=".$wxlintoken."&openid=".$ucode."&lang=zh_CN";
        $getutxtjson =json_decode($tools->http_curl_get($getutxturl,true));
        $uickname    =$getutxtjson->nickname;
        $usex        =intval($getutxtjson->sex);
        $headimgurl  =$getutxtjson->headimgurl;
        $nums=$dbconn->countn(DBQIAN."_user_list"," ucode='$ucode' and uid=1 ");
        if($nums==0){
            $times=time();
            $dbconn->noretquery("insert into ".DBQIAN."_user_list(ucode,uickname,usex,uheadimgurl,utime,uid)values
                     ('$ucode','$uickname',$usex,'$headimgurl',$times,1)");
        }
        else
        {
            $times=time();
            $dbconn->noretquery("update  ".DBQIAN."_user_list set uickname='$uickname',utime='$times' where ucode='$ucode' ");
        }

   }   
    elseif($ucode=='')
    {
        header("Location:index.php");exit();

    }  

    $_SESSION['ucode']=$ucode;
	//加分
    if($utocode)
    {
		$now_time=time();
		$data=$dbconn->fetch($dbconn->query("select * from ".DBQIAN."_score where ucode='".$ucode."'"));

		if(!$data){

			$sql = "insert into ".DBQIAN."_score (ucode,score_plus,time) values ('$ucode','5','$now_time')";
			$dbconn->noretquery($sql);
	    }
		else
		{
		   $dbconn->noretquery("update ".DBQIAN."_score set score_plus=5 where ucode='".$utocode."'");	
		}
     
    }

    $data=$dbconn->fetch($dbconn->query("select * from ".DBQIAN."_score where ucode='".$ucode."'"));
    $score=intval($data['score'])?intval($data['score']):0;

    // header("Location:mindex.html");exit;

  $utitle="我一口气接了".$score."个月饼，不来挑战，大奖就是我的啦!";
  $utitle_ct="中秋送礼，现金大奖等你拿";
    echo '
		
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		 <meta name="viewport" content="width=device-width,maximum-scale=1.0,user-scalable=no"/>
		<title>接月饼</title>
		<script src="lib/jquery-1.11.0.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <script>
      wx.config({   
        debug: false,
        appId: \'';echo $signPackage["appId"];;echo '\',
        timestamp: ';echo $signPackage["timestamp"];;echo ',
        nonceStr: \'';echo $signPackage["nonceStr"];;echo '\',
        signature: \'';echo $signPackage["signature"];;echo '\',
        jsApiList: [
            \'checkJsApi\',
            \'onMenuShareTimeline\',
            \'onMenuShareAppMessage\',
            \'onMenuShareQQ\',
            \'onMenuShareWeibo\',
            \'hideMenuItems\',
            \'showMenuItems\',
            \'hideAllNonBaseMenuItem\',
            \'showAllNonBaseMenuItem\',
            \'translateVoice\',
            \'startRecord\',
            \'stopRecord\',
            \'onRecordEnd\',
            \'playVoice\',
            \'pauseVoice\',
            \'stopVoice\',
            \'uploadVoice\',
            \'downloadVoice\',
            \'chooseImage\',
            \'previewImage\',
            \'uploadImage\',
            \'downloadImage\',
            \'getNetworkType\',
            \'openLocation\',
            \'getLocation\',
            \'hideOptionMenu\',
            \'showOptionMenu\',
            \'closeWindow\',
            \'scanQRCode\',
            \'chooseWXPay\',
            \'openProductSpecificView\',
            \'addCard\',
            \'chooseCard\',
            \'openCard\'
        ]
      });
      wx.checkJsApi({
          jsApiList: [
            \'getNetworkType\',
            \'previewImage\'
          ],
          success: function (res) {
            alert(JSON.stringify(res));
          }
      });
      wx.ready(function () {
        wx.onMenuShareTimeline({
             title: "';echo $utitle;;echo '",
             link: "';echo WEBNAME."main/index.php?utocode=".$ucode;;echo '",
             imgUrl: "';
    echo WEBNAME."main/images/order_gx.jpg";;echo '",
             trigger: function (res) {
             },
             success: function (res) {       
             },
             cancel: function (res) {            
             },
             fail: function (res) {
             }
        });
        wx.onMenuShareAppMessage({
            title: "';echo  $utitle;;echo '",
            desc: "';echo  $utitle_ct;;echo '",
            link: "';echo WEBNAME."main/index.php?utocode=".$ucode;;echo '",
            imgUrl: "';
    echo WEBNAME."main/images/order_gx.jpg";;echo '",
            trigger: function (res) {
            },
            success: function (res) {
            },
            cancel: function (res) {
            },
            fail: function (res) {
            }
         });
    });
    </script>
	
		<style>
		body{margin:0px; padding:0px; font-size:16px; font-family:"Microsoft YaHei" ! important;}
		ul{ list-style:none;}
		.ucontent{ width:100%;}
		.div_c1{position:fixed; width:90%;left:5%; z-index:2; bottom:1%;}
		</style>
		</head>
		
		<body>
		<div id="Layer1" style="position:fixed; width:100%; height:100%; z-index:-1">    
		<img src="images/index_wel.jpg" height="100%" width="100%"/>    
		</div>
		
		<div class="ucontent">
			<div class="div_c1" onClick="location.href=\'insert_user.php\'" >
			<img src="images/order_btinto3.png" width="100%"  height="100%"/>
			</div>
		   
		
		
		</div>
		</body>
		</html>
		';

	
?>